(function(){
    angular.module('app').controller("agbCustomerCtrl",function($scope,proxy,$stateParams){
        $scope.permission = PERMISSION;
        $scope.filter = {
            name:""
        }

        $scope.search = function(){
            getList();
        }

        $scope.gotoAdv = function (id) {
            
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/agency/chargeAdv',
                data:{
                    "advertiserID": id,
                },
                load: true,
                success: function(res) {
                    // 存下adv的token
                    localStorage["advToken"+id] = res.data;
                    // 存下当前代理商id
                    localStorage["advOrgID"+id] = $stateParams.agentID; 
                    window.open("./agentIndex.html#/agent/1/"+id+"/agIndex")
                }
            })
        }
        $scope.setStatus = function (item) {
            var adv = {
                id: item.id,
                state: item.open?"10":"30"
            }
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/advertiser/modify',
                load:true,
                data: {
                    data:adv
                },
                success:function(){
                    item.state = item.state == "10"?"20":"10";
                },
                error: function () {
                    // 失败时,将switch状态重置
                    item.open = !item.open
                }
            })
        }

        function getList(current, pagesize) {
            var data = {
                'page': (current || 1)+"",
                'size': (pagesize || 20)+"",
                'filter': $scope.filter,
                "sort": ["cTime,1"],
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/advertiser/putin/list',
                data: data,
                load:true,
                success: function(res){
                    // 获得列表
                    $scope.list = res.data.data;
                    $("#pageToolbar").html("");
                    $('#pageToolbar').Paging({
                        pagesize: pagesize || '20',
                        current: current || '1',
                        count:res.data.total,
                        toolbar:true,
                        'hash': false,
                        callback:getList
                    });
                }
            })
        }


        getList();
    })
})();